package testOne2Ten;

/**
 * 二维数组，每一个都按照从左到右递增，从上到下递增，输入一个整数判断存不存在
 * Created by Wongy on 2018/3/13.
 */
public class Test3 {
    private static int getNumInArray(Integer[][] array, int num) {
        if (array == null || array.length == 0) {
            return 0;
        }
        //统计
        int count = 0;
        //行数
        int rowCount = 0;
        //列数
        int colCount = array[0].length - 1;
        while (colCount >= 0 && rowCount < array.length) {
            if (array[rowCount][colCount] == num) {
                System.out.println("Test3.getNumInArray , 20 : 第" + (rowCount + 1) + "行 ， 第" + (colCount + 1) + "列。");
                count++;
                colCount--;
                rowCount++;
            } else if (array[rowCount][colCount] > num) {
                colCount--;
            } else {
                rowCount++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Integer[][] integers = new Integer[4][4];
        integers[0][0] = 1;
        integers[0][1] = 2;
        integers[0][2] = 8;
        integers[0][3] = 9;

        integers[1][0] = 2;
        integers[1][1] = 4;
        integers[1][2] = 9;
        integers[1][3] = 12;

        integers[2][0] = 4;
        integers[2][1] = 7;
        integers[2][2] = 10;
        integers[2][3] = 13;

        integers[3][0] = 7;
        integers[3][1] = 8;
        integers[3][2] = 11;
        integers[3][3] = 15;

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print(integers[i][j] + "    ");
            }
            System.out.println();
        }


        System.out.println(getNumInArray(integers, 7));
    }
}
